package com.ruoyi.app.controller.common.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.app.controller.common.service.ISysConfigService;
import com.ruoyi.app.controller.common.service.ITencentMapService;
import com.ruoyi.common.entity.BO.TencentMapAddressBO;
import com.ruoyi.common.constant.TencentMapConstants;
import com.ruoyi.common.enums.ConfigEnum;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;

@Slf4j
@Service
public class TencentMapServiceImpl implements ITencentMapService {

    @Autowired
    private ISysConfigService configService;

    /**
     * 详细地址解析为坐标
     *
     * @param address
     * @return
     */
    public JSONObject addressToLocation(String address) {
        String key = configService.selectConfigByKey(ConfigEnum.TENCENT_MAP_KEY.getKey());
        if (StringUtils.isEmpty(key)) {
            key = TencentMapConstants.KEY;
        }
        try {
            String param = "key=" + key + "&address=" + java.net.URLEncoder.encode(address, "utf-8");
            String returnJson = HttpUtils.sendGet(TencentMapConstants.ApiEnum.COMMON.getUrl(), param);
            JSONObject returnObject = JSON.parseObject(returnJson);
            if (returnObject.containsKey("status") && returnObject.containsKey("result")) {
                switch (TencentMapConstants.statusEnum.match(returnObject.getIntValue("status"))) {
                    case SUCCESS:
                        break;
                    default:
                        log.error(returnObject.getString("message"));
                        throw new BusinessException(TencentMapConstants.statusEnum.match(returnObject.getIntValue("status")).getErrorText());

                }
                return returnObject.getJSONObject("result");
            } else {
                throw new BusinessException("地址解析失败！");
            }
        } catch (JSONException e) {
            throw new BusinessException("返回地址数据解析失败！");
        } catch (UnsupportedEncodingException e) {
            throw new BusinessException("地址信息有误！");
        }
    }

    public JSONObject locationToAddress(String location) {
        String key = configService.selectConfigByKey(ConfigEnum.TENCENT_MAP_KEY.getKey());
        if (StringUtils.isEmpty(key)) {
            key = TencentMapConstants.KEY;
        }
        try {

            String param = "key=" + key + "&location=" + java.net.URLEncoder.encode(location, "utf-8");
            String returnJson = HttpUtils.sendGet(TencentMapConstants.ApiEnum.COMMON.getUrl(), param);
            JSONObject returnObject = JSON.parseObject(returnJson);
            if (returnObject.containsKey("status") && returnObject.containsKey("result")) {
                switch (TencentMapConstants.statusEnum.match(returnObject.getIntValue("status"))) {
                    case SUCCESS:
                        break;
                    default:
                        log.error(returnObject.getString("message"));
                        throw new BusinessException(TencentMapConstants.statusEnum.match(returnObject.getIntValue("status")).getErrorText());

                }
                return returnObject.getJSONObject("result");
            } else {
                throw new BusinessException("地址解析失败！");
            }
        } catch (JSONException e) {
            throw new BusinessException("返回地址数据解析失败！");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new BusinessException("返回地址数据解析失败！");
        }
    }

    @Override
    public TencentMapAddressBO getAddress(String address) {
        JSONObject infoObject = addressToLocation(address);
        if (infoObject.containsKey("address_components")) {
            return JSON.parseObject(infoObject.getString("address_components"), TencentMapAddressBO.class);
        } else {
            throw new BusinessException("地址信息填写有误！");
        }
    }

}
